package com.pwc.testing;

import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class SeleniumAction extends SeleniumBaseAction implements ISeleniumAction {

	public SeleniumAction(WebDriver driver, LogUtil log) {
		super(driver, log);
	}

	public void input(String findType, String element, String sendWords) {

		for (WebElement ele : findElements(findType, element)) {
			action.moveToElement(ele).click().sendKeys(sendWords);
			
			action.perform();
			log.addLog("input:" + sendWords + " to field:" + element + "," + findType);
		}

	}

	public void clear(String findType, String element) {
		for (WebElement ele : findElements(findType, element)) {

			ele.clear();
			log.addLog("clear:" + element);
		}

	}

	public void click(String findType, String element) {

		for (WebElement ele : findElements(findType, element)) {
			action.moveToElement(ele);
			action.click();
			action.perform();
			log.addLog("click:" + element + "," + findType);
		}

	}

	public void moveOver(String findType, String element, String findType2, String element2) {
		WebElement ele1 = findElements(findType, element).get(0);
		WebElement ele2 = findElements(findType2, element2).get(0);
		action.moveToElement(ele1).moveToElement(ele2);
		action.perform();
	}
	

	public void dbClick(String findType, String element) {
		for (WebElement ele : findElements(findType, element)) {
			action.doubleClick(ele).perform();
			log.addLog("double click:" + element + "," + findType);
		}
	}

	public void selectByIndex(String findType, String element, String index) {
		for (WebElement ele : findElements(findType, element)) {
			Select select = new Select(ele);
			select.selectByIndex(Integer.parseInt(index));
			log.addLog("select :" + element + ", by index" + index);
		}
	}

	public void selectByVisibleText(String findType, String element, String text) {
		for (WebElement ele : findElements(findType, element)) {
			Select select = new Select(ele);
			select.selectByVisibleText(text);
			log.addLog("select :" + element + ",by text" + text);
		}
	}

}
